package com.example.springbott.service;

import com.example.springbott.bean.MarketPermission;
import com.example.springbott.bean.MarketRole;
import com.example.springbott.bean.MarketRoleExample;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.kerbal.AdminRolePermissionsVo;
import com.example.springbott.bean.kerbal.RoleLabelVo;
import com.example.springbott.bean.kerbal.SystemPermissions;
import com.example.springbott.bean.kerbal.assignedPermissions;
import com.example.springbott.mapper.MarketApiMapper;
import com.example.springbott.mapper.MarketPermissionMapper;
import com.example.springbott.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class adminRoleServiceImpl implements adminRoleService {
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Autowired
    MarketApiMapper marketApiMapper;


    @Override
    public CommonData adminRoleOptions() {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        marketRoleExample.setOrderByClause("add_time " + "desc");
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        ArrayList<RoleLabelVo> roleLabelVos = new ArrayList<>();
        for (int i = 0; i < marketRoles.size(); i++) {
            RoleLabelVo roleLabelVo = new RoleLabelVo();
            roleLabelVo.setLabel(marketRoles.get(i).getName());
            roleLabelVo.setValue(marketRoles.get(i).getId());
            roleLabelVos.add(roleLabelVo);
        }

        PageInfo<RoleLabelVo> marketRolePageInfo = new PageInfo<>(roleLabelVos);
        return CommonData.data(marketRolePageInfo);
    }


    @Override
    public CommonData adminRoleList(BasePageInfo basePageInfo, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);
        return CommonData.data(marketRolePageInfo);

    }

    @Override
    @Transactional
    public MarketRole adminRoleCreate(@RequestBody String desc, String name) {
        MarketRole marketRole = new MarketRole();
        marketRole.setDesc(desc);
        marketRole.setName(name);
        marketRole.setAddTime(new Date());
        marketRole.setEnabled(true);
        marketRole.setDeleted(false);
        marketRoleMapper.insert(marketRole);


        return marketRole;


    }

    @Override
    @Transactional
    public Integer adminRoleUpdate(MarketRole marketRole) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());
        marketRole.setUpdateTime(new Date());
       return marketRoleMapper.updateByExample(marketRole, marketRoleExample);
    }

    @Override
    public Integer adminRoleDelete(MarketRole marketRole) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());
        marketRole.setUpdateTime(new Date());
        marketRole.setDeleted(true);
        return marketRoleMapper.updateByExample(marketRole,marketRoleExample);


    }

    @Override
    public AdminRolePermissionsVo adminRolePermissions(Integer roleId) {
        AdminRolePermissionsVo adminRolePermissionsVo = new AdminRolePermissionsVo();
        assignedPermissions assignedPermissions = new assignedPermissions();

        if (roleId!=1){
            adminRolePermissionsVo.setAssignedPermissions(marketRoleMapper.SelectassignedPermissions(roleId));
        }else {
            adminRolePermissionsVo.setAssignedPermissions(marketApiMapper.selectAllPermissions());
        }

        List<SystemPermissions.SystemPermissionsDTO> systemPermissionsDTOS = marketRoleMapper.SelectsystemPermissions();
        adminRolePermissionsVo.setSystemPermissions(systemPermissionsDTOS);
        return adminRolePermissionsVo;
    }

    @Override
    @Transactional
    public Integer adminRolePermissionsGet(String[] permissions, Integer roleId) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(roleId);

        marketRoleMapper.updateDelete(roleId);
        for (int i = 0; i < permissions.length; i++) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setRoleId(roleId);
            marketPermission.setPermission(permissions[i]);
            marketPermission.setAddTime(new Date());
            marketPermission.setDeleted(false);
            marketPermissionMapper.insert(marketPermission);
        }
        return 200;
    }


}
